package com.mostone.push.demo

import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.mostone.push.sdk.MOPush
import com.mostone.push.sdk.base.listener.IMOPushCallBack
import com.mostone.push.sdk.listener.MORegisterListener
import com.mostone.push.sdk.listener.MOUnRegisterListener
import com.mostone.push.sdk.model.MOUnBindAliasOption
import kotlinx.android.synthetic.main.activity_main.*
import java.util.concurrent.atomic.AtomicInteger

class MainActivity : AppCompatActivity() {
    private val REQUEST_EXTERNAL_STORAGE = 1
    private val PERMISSIONS_STORAGE = arrayOf(
        "android.permission.READ_EXTERNAL_STORAGE",
        "android.permission.WRITE_EXTERNAL_STORAGE"
    )


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //请求下存储权限
        verifyStoragePermissions(this)
        initClick()
        //  tv_build.text = "编译时间: ${BuildConfig.VERSION_DATE}"
    }

    private fun initClick() {
        val count = AtomicInteger(1)
        bt_init.setOnClickListener {
            //初始化
        }

        bt_register.setOnClickListener {
            //注册设备
            MOPush.instance.register(object : MORegisterListener {
                override fun onFail(code: Int, errorMsg: String) {
                    runOnUiThread {

                        tv_device.text = "请求失败 code:$code     errorMsg:$errorMsg"
                    }
                }

                override fun onSuss(token: String) {
                    Log.d("默往推送", "设备id   $token")
                    runOnUiThread {
                        tv_device.text = "注册成功  设备id:$token"
                    }
                }
            })
        }

        bt_un_register.setOnClickListener {
            //解除设备注册
            MOPush.instance.unRegister(object : MOUnRegisterListener {
                override fun onFail(code: Int, errorMsg: String) {
                    runOnUiThread {
                        tv_device.text = "请求失败 code:$code     errorMsg:$errorMsg"
                    }
                }

                override fun onSuss() {
                    runOnUiThread {
                        tv_device.text = "取消成功  "
                    }
                }
            })
        }

        bt_device.setOnClickListener {
            tv_device.text = "设备id: ${MOPush.instance.getDeviceId()}"
            Log.d("默往推送", "设备id   ${MOPush.instance.getDeviceId()}  ${MOPush.instance.getBrand()}")
        }

        bt_bind_alias.setOnClickListener {
            //绑定别名
            val alias = et_alias.text.toString()
            //判断是否绑定别名同时是否删除此别名下的其他绑定设备
            val isDelOther = cb_alias.isChecked
            MOPush.instance.bindAlias(alias, isDelOther, object : IMOPushCallBack {
                override fun onFail(code: Int, errorMsg: String) {
                    runOnUiThread {
                        Toast.makeText(
                            this@MainActivity,
                            "绑定失败 $code   $errorMsg",
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                }

                override fun onSuss() {
                    runOnUiThread {
                        Toast.makeText(this@MainActivity, "绑定成功", Toast.LENGTH_SHORT).show()
                    }
                }
            })
        }

        bt_unbind_alias.setOnClickListener {
            //解除别名绑定
            val option = when (rg_unbind.checkedRadioButtonId) {
                R.id.rb_del_other -> MOUnBindAliasOption.DELETE_OTHERS//删除别名下其他绑定的设备
                R.id.rb_del_self -> MOUnBindAliasOption.DELETE_SELF//删除当前设备绑定的别名
                else -> MOUnBindAliasOption.DELETE_ALL//删除此设备别名的绑定的所有设备
            }
            MOPush.instance.unBindAlias(option, object : IMOPushCallBack {
                override fun onFail(code: Int, errorMsg: String) {
                    runOnUiThread {
                        Toast.makeText(
                            this@MainActivity,
                            "解绑别名失败 $code   $errorMsg",
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                }

                override fun onSuss() {
                    runOnUiThread {
                        Toast.makeText(this@MainActivity, "解绑别名成功", Toast.LENGTH_SHORT).show()
                    }
                }

            })
        }
    }

    private fun verifyStoragePermissions(activity: Activity?) {
        try {
            val permission = ActivityCompat.checkSelfPermission(
                activity!!,
                "android.permission.WRITE_EXTERNAL_STORAGE"
            )
            if (permission != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(
                    activity,
                    PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE
                )
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}